﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.ComponentModel.Composition;
using WaterRevenueMS.WebUI.Extension.Filters;
using System.Linq.Expressions;
using WaterRevenueMS.Componnent.Entities.ReadMeterMS;
using WaterRevenueMS.WebUI.Extension.MvcHelper;
using WaterRevenueMS.Domain.Common.Models;
using WaterRevenueMS.WebUI.Common;
using WaterRevenueMS.Core.Service.ReadMeterMS;
using WaterRevenueMS.Domain.ViewModels.ReadMeterMS.WaterChargeChange;

namespace WaterRevenueMS.WebUI.Controllers
{
    [Export]
    [PartCreationPolicy(CreationPolicy.NonShared)]
    public class WaterChargeChangeController : AdminController
    {
        [Import]
        private IWaterChargeChangeService WaterChargeChangeService { get; set; }
        
        [AdminLayoutAttribute]
        public ActionResult Index()
        {
            var model = new WaterChargeChangeModel();
            return View(model);
        }

        [AdminPermission(PermissionCustomMode.Ignore)]
        public ActionResult List(DataTableParameter param)
        {
            int total = WaterChargeChangeService.WaterChargeChanges.Count(t => t.IsDeleted == false);

            var expr = BuildSearchCriteria();

            var filterResult = WaterChargeChangeService.WaterChargeChanges.Where(expr).Select(t => new WaterChargeChangeModel
            {
                Id = t.Id,
                MeterNo=t.MeterNo,
                Fei_Y=t.Fei_Y

            }).OrderBy(t => t.Id).Skip(param.iDisplayStart).Take(param.iDisplayLength).ToList();

            int sortId = param.iDisplayStart + 1;

            var result = from c in filterResult
                         select new[]
                        {
                            sortId++.ToString(), 
                            c.MeterNo.ToString(),
                            c.Fei_Y.ToString(),
                            c.Id.ToString()
                        };

            return Json(new
            {
                sEcho = param.sEcho,
                iDisplayStart = param.iDisplayStart,
                iTotalRecords = total,
                iTotalDisplayRecords = total,
                aaData = result
            }, JsonRequestBehavior.AllowGet);
        }

        private Expression<Func<WaterChargeChange, Boolean>> BuildSearchCriteria()
        {
            DynamicLambda<WaterChargeChange> bulider = new DynamicLambda<WaterChargeChange>();
            Expression<Func<WaterChargeChange, Boolean>> expr = null;
            if (!string.IsNullOrEmpty(Request["StartTime"]))
            {
                var data = Convert.ToDateTime(Request["StartTime"].Trim());
                Expression<Func<WaterChargeChange, Boolean>> tmp = t => t.CreateDT >= data;
                expr = bulider.BuildQueryAnd(expr, tmp);
            }
            if (!string.IsNullOrEmpty(Request["EndTime"]))
            {
                var data = Convert.ToDateTime(Request["EndTime"].Trim());
                Expression<Func<WaterChargeChange, Boolean>> tmp = t => t.CreateDT <= data;
                expr = bulider.BuildQueryAnd(expr, tmp);
            }
            Expression<Func<WaterChargeChange, Boolean>> tmpSolid = t => t.IsDeleted == false;
            expr = bulider.BuildQueryAnd(expr, tmpSolid);

            return expr;
        }

    }
}
